package com.zhs.demoutil.demo1;

import java.util.ArrayList;
import java.util.List;
/*
请使用Java语言实现如下税率计算：
1~5000 税率 0
5001~8000 3%
8001~17000 10%
17001~30000 20%
30001~40000 25%
40001~60000 30%
60001~85000 35%
85001~ 45%
要求
    ⅰ. 逻辑正确，代码优雅
    ⅱ. 可扩展性，考虑区间的变化，比如说起征点从5000变成10000等等，或者说85000以上的征税50%。
    ⅲ. 推荐使用合适的设计模式
这里举个例子，比如说税前10000元，5000部分是不扣税，后面5000，3000扣税3%，2000扣税10%。
public class TaxCalculator {

    public double calculateTax(int amount) {
// TODO

    }

}
 */
class TaxBracket {
    private double lowerBound;
    private double upperBound;
    private double rate;

    public TaxBracket(double lowerBound, double upperBound, double rate) {
        this.lowerBound = lowerBound;
        this.upperBound = upperBound;
        this.rate = rate;
    }

    public double getLowerBound() {
        return lowerBound;
    }

    public double getUpperBound() {
        return upperBound;
    }

    public double getRate() {
        return rate;
    }
}

class TaxCalculator {
    private List<TaxBracket> brackets;

    public TaxCalculator() {
        brackets = new ArrayList<>();
        brackets.add(new TaxBracket(0, 5000, 0));
        brackets.add(new TaxBracket(5000, 8000, 0.03));
        brackets.add(new TaxBracket(8000, 17000, 0.1));
        brackets.add(new TaxBracket(17000, 30000, 0.2));
        brackets.add(new TaxBracket(30000, 40000, 0.25));
        brackets.add(new TaxBracket(40000, 60000, 0.3));
        brackets.add(new TaxBracket(60000, 85000, 0.35));
        brackets.add(new TaxBracket(85000, Double.POSITIVE_INFINITY, 0.45));
    }

    public double calculateTax(int amount) {
        double totalTax = 0.0;
        for (TaxBracket bracket : brackets) {
            double taxableAmount = Math.min(amount, bracket.getUpperBound()) - bracket.getLowerBound();
            if (taxableAmount <= 0) {
                break;
            }
            double tax = taxableAmount * bracket.getRate();
            totalTax += tax;
        }
        return totalTax;
    }
}

public class Main {
    public static void main(String[] args) {
        TaxCalculator calculator = new TaxCalculator();

        int amount = 10000;
        double tax = calculator.calculateTax(amount);
        System.out.println("税前收入：" + amount + " 元");
        System.out.println("应纳税额：" + tax + " 元");
    }
}